# -*- coding: utf-8 -*-

@check_session
def index():
    shop = sip.taobao.shop.get(fields=['sid','cid','title','desc','bulletin','pic_path','create','modified'], nick='tel800810')
    shops= shop['rsp']['shops']
#    测试店铺橱窗 api
#    showcase = sip.taobao.shop.showcase.remainCount()
#    logger('showcase', showcase)
    revise_shop = FORM(TABLE(
        TR(SPAN("修改名称:", _style='font-size: 75%'), INPUT(_type='text', _name='title')),
        TR(SPAN("修改公告:", _style='font-size: 75%'), TEXTAREA(_rows="13", _cols="50", _name='bulletin')),
        TR(SPAN("修改店铺介绍:", _style='font-size: 75%'), TEXTAREA(_rows="13", _cols="50", _name='desc')),
        TR('',INPUT(_type='submit', _value='提交修改', _sytle='padding: 0 5px;'))
        ))

#    使用 up 完成更新
#    if revise_shop.accepts(request.vars, session):
#        redirect(URL(r=request, f='up', vars={'title':request.vars.title, 'bulletin':request.vars.bulletin}))

    if revise_shop.accepts(request.vars, session):
        x = [i for i in request.vars.keys() if '_' not in i]
        keys = [i for i in x if request.vars[i]]
        if not keys:
            redirect(URL(r=request))
        v = ''
        for key in keys:
            v += '%s=request.vars.%s,' % (key, key)
        exec('sip.taobao.shop.update(%s)') % v
        redirect(URL(r=request, f='index'))
        
    return dict(
        shops = shops,
        revise_shop = revise_shop,
        )

def up():
    """ 在 index 中完成，up 已不用！"""
    x = request.vars.keys()
    keys = [i for i in x if request.vars[i]]
    if not keys:
        redirect(URL(r=request, f='index'))
    v = ''
    for key in keys:
        v += ', %s="%s"' % (key, request.vars[key])
    exec('sip.taobao.shop.update(%s)') % v
    redirect(URL(r=request, f='index'))
